// pages/shop/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    goodslist: [],
    totalPrice: 0,
    total: 0,
    allChecked: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  // 点击获取收货地址按钮执行的方法
  chooseAdress() {
    wx.getSetting({
      success: (result) => {
        // console.log(result);
        let scopedAddress = result.authSetting["scoped.Address"]
        if (scopedAddress == true || scopedAddress == undefined) {
          this.getAddress();
        } else {
          wx.openSetting({
            success: (result2) => {
              this.getAddress()
            },
          });

        }
      },

    });
  },

  //获取到收货地址
  getAddress() {
    wx.chooseAddress({
      success: (result) => {
        let address = result
        address.allAddress = result.provinceName + result.cityName + result.countyName + result.detailInfo
        this.setData({
          address
        })
        wx.setStorageSync("address", address);

      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let goodslist = wx.getStorageSync("goodslist") || [];
    let address = wx.getStorageSync("address") || {};
    this.setData({
      address,
      goodslist
    })
    this.setGoodslist(goodslist)
  },

  // 商品选择按钮
  changechecked(e) {
    let { id } = e.currentTarget.dataset
    let { goodslist } = this.data
    let index = goodslist.findIndex(v => v.goods_id === id);
    // 4 选中状态取反
    goodslist[index].checked = !goodslist[index].checked;
    this.setGoodslist(goodslist);

  },
  // 修改商品数量
  Modify(e) {
    let { id, number } = e.currentTarget.dataset;
    let { goodslist } = this.data
    let index = goodslist.findIndex(v => v.goods_id === id);
    if (goodslist[index].count == 1 && number == -1) {
      wx.showModal({
        title: '提示',
        content: "您确定要删除该商品？",
        success: (res) => {
          if (res.confirm) {
            goodslist.splice(index, 1);
            this.setGoodslist(goodslist)
          } else if (res.cancel) {

          }
        }
      })
    } else {
      goodslist[index].count += number;
      this.setGoodslist(goodslist)
    }

  },



  // 全选按钮
  changeSelect() {
    let { allChecked, goodslist } = this.data
    goodslist.forEach(item => {
      item.checked = !allChecked
    });
    this.setData({
      goodslist,
      allChecked: !allChecked
    })
    this.setGoodslist(goodslist)
  },

  // 更新商品信息和总价格等数据
  setGoodslist(goodslist) {
    let { allChecked } = this.data
    let total = 0
    let totalPrice = 0
    goodslist.length == 0 ? this.setData({
      allChecked: false,
      goodslist: [],
      total,
      totalPrice
    }) : ""
    goodslist.forEach((item, index) => {
      allChecked = true
      if (!item.checked) {
        allChecked = false;
      } else {
        total += item.count;
        totalPrice += item.count * item.goods_price
      }
      this.setData({
        allChecked,
        goodslist,
        total,
        totalPrice
      })

    })
    wx.setStorageSync("goodslist", this.data.goodslist);
  },

  // 跳转支付
  gopay() {
    // 判断是否有收货地址
    if (!this.data.address.userName) {
      wx.showToast({
        title: '请先选择收获地址',
        icon: "none",
        duration: 1000
      })
      return;
    }
    // 判断有没有商品
    if (this.data.goodslist.length == 0) {
      wx.showToast({
        title: '请先选择商品',
        icon: "none",
        duration: 1000
      })
      return;
    }
    // 3 跳转到 支付页面
    wx.navigateTo({
      url: '/pages/pay/index'
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})